package com.controller.purchase;

import java.io.IOException;
import java.net.URLEncoder;
import java.rmi.RemoteException;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.google.protobuf.ServiceException;
import com.model.purchase.OrderModel;
import com.model.purchase.OrderVoModel;
import com.service.purchase.OrderService;

/**
 * 系统日志表
 */
@Controller("orderController")
@RequestMapping(value = "/api")
public class OrderController {
	static final Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	OrderService orderService;

	/**
	 * 查询列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/order/selectList", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult selectList(@RequestParam Map<String, Object> map) {
		return orderService.selectList(map);
	}

	/**
	 * 查询详情
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "purchase/order/selectInfor", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult selectInfor(@RequestParam String id) {
		return orderService.selectInfor(id);
	}

	@RequestMapping(value = "purchase/order/transfer", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult transfer(@RequestParam String userId, @RequestParam String ordId) {
		return orderService.transfer(userId, ordId);
	}

	@RequestMapping(value = "purchase/order/change", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult change(@RequestParam String contractEnd, @RequestParam String ordId) {
		return orderService.change(contractEnd, ordId);
	}

	@RequestMapping(value = "/purchase/order/backAdmin", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult backAdmin(@RequestBody OrderVoModel orderModel) {
		return orderService.backAdmin(orderModel);
	}

	/**
	 * 根据询价结果生成采购订单
	 * 
	 * @param
	 * @return
	 * @throws ServiceException
	 * @throws RemoteException
	 */
	@RequestMapping(value = "purchase/order/createOrder", method = { RequestMethod.GET })
	@ResponseBody
	public HttpResult createOrder(@RequestParam String resId) {
		return orderService.createOrder(resId);
	}

	/**
	 * 保存订单
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "purchase/order/save", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult save(@RequestBody OrderVoModel orderModel) throws Exception {
		return orderService.save(orderModel);
	}

	/**
	 * 提交订单
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "purchase/order/commit", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult commit(@RequestBody OrderVoModel orderModel) throws Exception {
		return orderService.commit(orderModel);
	}

	@RequestMapping(value = "purchase/order/sendOa", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult sendOa(@RequestBody OrderVoModel orderModel) throws Exception {
		return orderService.sendOa(orderModel);
	}

	/**
	 * 特殊订单
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "purchase/order/saveSpecial", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult saveSpecial(@RequestBody OrderVoModel orderModel) throws Exception {
		return orderService.saveSpecial(orderModel);
	}

	/**
	 * 撤回订单
	 * 
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "purchase/order/back", method = { RequestMethod.POST })
	@ResponseBody
	public HttpResult back(@RequestBody OrderModel orderModel) throws Exception {
		return orderService.back(orderModel);
	}

	/**
	 * 删除订单
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "purchase/order/delete", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult delete(@RequestParam String id) {
		return orderService.delete(id);
	}

	@RequestMapping(value = "purchase/order/deleteSpecial", method = { RequestMethod.DELETE })
	@ResponseBody
	public HttpResult deleteSpecial(@RequestParam String ordId, @RequestParam String resId) {
		return orderService.deleteSpecial(ordId, resId);
	}

	/**
	 * 导出
	 * 
	 * @param map
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping(value = "purchase/order/export")
	public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
		orderService.export(map, response);

	}
}
